/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

public class ImageHelper {

    private static Logger logger = Logger.getLogger(ImageHelper.class);     
    
    private double width;
    private double height;
	private double originalWidth;
	private double originalHeight;
	private File file;
	private Image image;
	private byte[] imageBytes;

	public ImageHelper() {
		reset();
	}
	
	public void reset() {
		width =0;
		height =0;
		originalWidth =0;
		originalHeight =0;
		file =null;
		image =null;
		imageBytes =null;
	}

    /**
     * @return Returns the file.
     */
    public File getFile() {
        return file;
    }
    
    public void setFile( File file ) {
    	this.file =file;
    }
    
    /**
     * @param file The file to load.
     */
    public void loadFile(File file) throws IOException {
        this.file = file;
        
        InputStream is = new FileInputStream(file);
        
        long length = file.length();

        imageBytes = new byte[(int)length];

        int offset = 0;
        int numRead = 0;
        while (offset < imageBytes.length
               && (numRead=is.read(imageBytes, offset, imageBytes.length-offset)) >= 0) {
            offset += numRead;
        }

        if (offset < imageBytes.length) {
            throw new IOException( "Could not completely read file " + file.getName() );
        }

        is.close();      
        
        image =new ImageIcon( imageBytes ).getImage();
         
        width =image.getWidth( null );
        originalWidth =width;
        height =image.getHeight( null );
        originalHeight =height;
    }
    
	public Image computeResizedImage( double newWidth, double newHeight ) {
    	BufferedImage resizedImg = new BufferedImage( (int)newWidth, (int)newHeight, BufferedImage.TYPE_INT_ARGB);
    	Graphics2D newg2d = (Graphics2D)resizedImg.getGraphics();
    	newg2d.scale( newWidth / (double)image.getWidth(null), newHeight / (double)image.getHeight(null));
    	newg2d.drawImage( image, 0,0 , null );
    	
    	return resizedImg;
	}    
    
	public void resetToOriginalSize() {
	    width =originalWidth;
	    height =originalHeight;
	}    
    
    public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public byte[] getImageBytes() {
		return imageBytes;
	}

	public void setImageBytes(byte[] imageBytes) {
		this.imageBytes = imageBytes;
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public double getOriginalWidth() {
		return originalWidth;
	}

	public void setOriginalWidth(double originalWidth) {
		this.originalWidth = originalWidth;
	}

	public double getOriginalHeight() {
		return originalHeight;
	}

	public void setOriginalHeight(double originalHeight) {
		this.originalHeight = originalHeight;
	}	

}
